﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DTheme
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:DTheme"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根
    /// 元素中:
    ///
    ///     xmlns:MyNamespace="clr-namespace:DTheme;assembly=DTheme"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误:
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:DTextBoxCheck/>
    ///
    /// </summary>
    public class DTextBoxCheck : TextBox
    {
        static DTextBoxCheck()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DTextBoxCheck), new FrameworkPropertyMetadata(typeof(DTextBoxCheck)));
        }
        public DTextBoxCheck()
        {
            this.TextChanged += check;
        }
        public enum EnumTextType { None, String, Double, Int, Double2, Double3 }
        public CornerRadius CornerRadius { get { return (CornerRadius)GetValue(CornerRadiusProperty); } set { SetValue(CornerRadiusProperty, value); } }
        public static readonly DependencyProperty CornerRadiusProperty
            = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(DTextBoxCheck));
        public EnumTextType TextType { get { return (EnumTextType)GetValue(TextTypeProperty); } set { SetValue(TextTypeProperty, value); } }
        public static readonly DependencyProperty TextTypeProperty
            = DependencyProperty.Register("TextType", typeof(EnumTextType), typeof(DTextBoxCheck), new PropertyMetadata(EnumTextType.None));
        public string ErrorMessage
        {
            get { return (string)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ErrorMessage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register("ErrorMessage", typeof(string), typeof(DTextBoxCheck), new PropertyMetadata(""));

        public Visibility ErrorMessageVisibility
        {
            get { return (Visibility)GetValue(ErrorMessageVisibilityProperty); }
            set { SetValue(ErrorMessageVisibilityProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ErrorMessageVisibility.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ErrorMessageVisibilityProperty =
            DependencyProperty.Register("ErrorMessageVisibility", typeof(Visibility), typeof(DTextBoxCheck), new PropertyMetadata(Visibility.Collapsed));


        public Func<string, string> AppendRule { get { return (Func<string, string>)GetValue(AppendRuleProperty); } set { SetValue(AppendRuleProperty, value); } }
        public static readonly DependencyProperty AppendRuleProperty
            = DependencyProperty.Register("AppendRule", typeof(Func<string, string>), typeof(DTextBoxCheck));

        private void check(object sender, TextChangedEventArgs e)
        {
            bool isValid=true;
            switch (TextType)
            {
                case EnumTextType.Double:
                    if (!double.TryParse(Text, out double tempa)) { ErrorMessage = "非有效数字"; isValid = false;  }
                    break;
                case EnumTextType.Int:
                    if (!int.TryParse(Text, out int tempb)) { ErrorMessage = "非有效整数"; isValid = false; }
                    break;
                case EnumTextType.String:
                    if (string .IsNullOrEmpty(Text)){ ErrorMessage = "不能为空"; isValid = false; }
                    break;
                case EnumTextType.Double2:
                    string t2 = Text.Replace('，', ',');                  
                    string[] s2 = t2.Split(',');
                    if (s2.Length == 2)
                    {
                        double x, y;
                        if (double.TryParse(s2[0], out x) && double.TryParse(s2[1], out y) )
                        {
                            break;
                        }
                    }
                    ErrorMessage = "格式如：0,0";
                    isValid = false;
                    break;
                case EnumTextType.Double3:
                    string t3 = Text.Replace("，", ",");
                    string[] s3 = t3.Split(',');
                    if (s3.Length == 3)
                    {
                        double x, y, z;
                        if (double.TryParse(s3[0], out x) && double.TryParse(s3[1], out y) && double.TryParse(s3[2], out z))
                        {
                            break;
                        }
                    }
                    ErrorMessage = "格式如：0,0,0";
                    isValid = false;
                    break;
                default:
                    break;
            }
            if (isValid && AppendRule != null)
            {
                string appendMsg = AppendRule(Text);
                if (!string.IsNullOrEmpty(appendMsg))
                {
                    ErrorMessage = appendMsg;
                    isValid = false;
                }
            }
            if (isValid) 
            {
                ErrorMessageVisibility = Visibility.Collapsed;
                ErrorMessage = "";
            }else
            {
                ErrorMessageVisibility = Visibility.Visible;
            }
        }
 
    }
}
